package com.example.com.skills_utf8;

import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.app.Fragment;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.BaseFragment;
import com.app.util.Utils_Hardware;


//
public class _64_5Fragment extends Fragment {


    private final String TAG = _64_5Fragment.class.getSimpleName();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_64_5, null);
        InitView(view);

        return view;
    }


    public void InitView(View view) {

        final Button btn_dest = (Button)view.findViewById(R.id.btn_dest);
        final Button btn_scale_trans = (Button)view.findViewById(R.id.btn_scale_trans);


         /*
        int[][] value = new int[2][2];
        value[0][0] = 0;
        value[0][1] = 400 - 100;
        value[1][0] = 0;
        value[1][1] = 400 - 100;

        ObjectAnimator animator = ObjectAnimator.ofMultiInt(btn_dest, "xyz",  value );  // mini sdk 21
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

            }
        });
        */

        {
            // http://blog.csdn.net/lmj623565791/article/details/38067475
            ValueAnimator valueAnimator = new ValueAnimator();
            valueAnimator.setDuration(3000);
            valueAnimator.setObjectValues(new PointF(0, 0));
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
                @Override
                public PointF evaluate(float fraction, PointF startValue,
                                       PointF endValue) {


                    float speed = 200;  //

                    float t = fraction * 3;
                    Log.e(TAG, t + "");
                    PointF point = new PointF();
                    point.x = speed * t;
                    point.y = 0.5f * speed * (t) * (t);
                    return point;
                }
            });

            valueAnimator.start();
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    PointF point = (PointF) animation.getAnimatedValue();
                    btn_dest.setX(point.x);
                    btn_dest.setY(point.y);

                }
            });

        }





        {
            btn_scale_trans.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    final Rect rect = new Rect();
                    int[] v_LocationOnScreen = new int[2];


                    //v.getLocalVisibleRect(rect);
                    v.getLocationOnScreen(v_LocationOnScreen);
                    //v.getHitRect(rect);
                    final int[] screen = Utils_Hardware.getScreenSize(getActivity());
                    final int src_x = v_LocationOnScreen[0]; //rect.centerX();
                    final int src_y = v_LocationOnScreen[1]; //rect.centerY();

                    Log.e(TAG, "rect: "+rect.toString());



                    ValueAnimator valueAnimator = new ValueAnimator();
                    valueAnimator.setDuration(3000);
                    valueAnimator.setObjectValues(new Bean());
                    valueAnimator.setInterpolator(new LinearInterpolator());
                    valueAnimator.setEvaluator(new TypeEvaluator<Bean>() {
                        @Override
                        public Bean evaluate(float fraction, Bean startValue,
                                             Bean endValue) {

                            Bean bean = new Bean();
                            PointF point1 = new PointF();
                            PointF point2 = new PointF();

                            float delta_x = screen[0]/2 - src_x;
                            float delta_y = screen[1]/2 - src_y;

                            bean.trans.x = delta_x * fraction;
                            bean.trans.y = delta_y * fraction;
                            bean.scale.x = (1.5f - fraction); //不要小于0.5f  //fraction * 2;// //[0-1]
                            bean.scale.y = (1.5f - fraction); //fraction * 2;//(1-fraction);


                            Log.e(TAG, "-trans.x: " + bean.trans.x + " trans.y: " + bean.trans.y);
                            Log.e(TAG, "---scale.x: " + bean.scale.x + " scale.y: " + bean.scale.y);


                            return bean;
                        }
                    });

                    valueAnimator.start();
                    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            Bean bean = (Bean) animation.getAnimatedValue();

                            Matrix matrix = new Matrix();
                            RectF rect_dest = new RectF();
                            rect_dest.set(rect);   // 定位到源矩形位置
                            matrix.mapRect(rect_dest);  // 变换该矩形，使其在目标处显示



                            btn_scale_trans.setTranslationX(bean.trans.x);
                            btn_scale_trans.setTranslationY(bean.trans.y);

                            btn_scale_trans.setScaleX(bean.scale.x);
                            btn_scale_trans.setScaleY(bean.scale.y);

                        }
                    });




                }
            });




        }







    }




    class Bean
    {
        PointF trans;
        PointF scale;

        public Bean(){
            trans = new PointF(0,0);
            scale = new PointF(1,1);
        }

    }




}
